﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class PartnerApiController : ApiController
    {
        public PartnerRepository Get(int? pageNumber, string orderBy, string orderDirection, string partnerCode, string partnerName)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(partnerCode))
                    whereClause = String.Format(whereClause + " AND PartnerCode.Contains(\"{0}\")", partnerCode);
                if (!String.IsNullOrWhiteSpace(partnerName))
                    whereClause = String.Format(whereClause + " AND PartnerName.Contains(\"{0}\")", partnerName);

                return new PartnerRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Partner Get(int? id)
        {
            try
            {
                if (!id.HasValue || id == 0) return new Partner();

                var partnerRepository = new PartnerRepository();
                var partner = partnerRepository.GetByID(id.Value);

                return partner;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var partnerRepository = new PartnerRepository();
                    partnerRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public PartnerRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string partnerCode, string partnerName)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var partnerRepository = new PartnerRepository();
                partnerRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection, partnerCode, partnerName);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Partner Put(Partner partner)
        {
            try
            {
                var partnerRepository = new PartnerRepository();
                int partnerID = partnerRepository.Update(partner);

                return Get(partnerID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }

        public List<PartnerComplex> Get()
        {
            try
            {
                return new PartnerRepository().GetListPartnerComplex();
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        } 
    }
}
